﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using SCMS.Model;
using System.Data.Objects.DataClasses;

namespace SCMS.UI.Models
{
    public class SalaryPayment
    {
        public Model.SalaryRequest EntitySalaryP { get; set; }

        public SelectList Currencies { get; set; }

        public SelectList Months { get; set; }

        public SelectList Offices { get; set; }

        public List<SalaryPaymentItems> SalaryPaymentItems { get; set; }

        [Required(ErrorMessage="Master Budget Currency is required.")]
        public Guid MBCurrencyId { get; set; }

        [Required(ErrorMessage="Master Budget Value is required.")]
        public decimal MBValue { get; set; }
        
    }

    public class SalaryPaymentItems
    {
        public Model.SalaryBudgetline EntitySalaryPBL { get; set; }

        public SelectList ProjectNos { get; set; }

        public Guid ProjectDonorId { get; set; }

        public SelectList BudgetLines { get; set; }

        public Guid BudgetLineId { get; set; }

        public SelectList Items { get; set; }
    }

    public class SalaryPItems
    {
        public Model.SalaryBudgetline EntitySalaryPBL { get; set; }
    }

    public class ViewSalaryPayment
    {
        public Model.SalaryRequest EntitySalaryP { get; set; }

        public Model.VStaffDetail PreparedBy { get; set; }

        public Model.VStaffDetail ApprovedBy { get; set; }

        public Model.VStaffDetail AuthorizedBy { get; set; }

        public bool CanApprove { get; set; }

        public bool CanPostFunds { get; set; }
    }


    public class SalaryPaymentExtension
    {
        public static ViewSalaryPayment PrepareSP(Guid SPId)
        {
            using (var db = new SCMSEntities())
            {
                SalaryRequest SPayment = db.SalaryRequests.FirstOrDefault(p => p.Id == SPId);
                Currency c = SPayment.Currency;
                CountrySubOffice cbo = SPayment.CountrySubOffice;
                Location loc = cbo.Location;
                if (SPayment.Staff1 != null)
                {
                    var person = SPayment.Staff1.Person;
                }
                if (SPayment.Staff2 != null)
                {
                    var person = SPayment.Staff2.Person;
                }
                if (SPayment.Staff3 != null)
                {
                    var person = SPayment.Staff3.Person;
                }
                EntityCollection<SalaryBudgetline> sbl = SPayment.SalaryBudgetlines;
                foreach (SalaryBudgetline sblitem in sbl)
                {
                    ProjectDonor pd = sblitem.ProjectBudget.BudgetCategory.ProjectDonor;
                    PayrollItem pit = sblitem.PayrollItem;

                }
                var model = new ViewSalaryPayment()
                {
                    EntitySalaryP = SPayment,
                    PreparedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == SPayment.PreparedBy),
                    ApprovedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == SPayment.ReviewedBy),
                    AuthorizedBy = db.VStaffDetails.FirstOrDefault(p => p.StaffID == SPayment.AuthorizedBy)
                };
                return model;
            }
        }
    }

    public class RejectSPM
    {
        [Required]
        public Guid Id { get; set; }

        public string RefNumber { get; set; }

        [Required(ErrorMessage = "Remarks are required.")]
        public string RejectedAuthRemarks { get; set; }

        [Required(ErrorMessage = "Remarks are required.")]
        public string RejectedReviewRemarks { get; set; }

        public bool IsReview { get; set; }
    }

    public class RejectTAF
    {
        [Required]
        public Guid Id { get; set; }

        public string RefNumber { get; set; }

        [Required(ErrorMessage = "Remarks are required.")]
        public string RejectedAuthRemarks { get; set; }

        [Required(ErrorMessage = "Remarks are required.")]
        public string RejectedReviewRemarks { get; set; }

        public bool IsReview { get; set; }
    }
}